// components/order/evaluate/evaluate.js
import {
  find
} from '../../../api/order';
import {
  save
} from "../../../api/review"
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    riderStatus: true,
    riderTag: [
      { title: '风雨无阻', type: false },
      { title: '快速准时', type: false },
      { title: '穿戴工服', type: false },
      { title: '货品完好', type: false },
      { title: '礼貌热情', type: false },
      { title: '仪表整洁', type: false }
    ],
    satisfiedChooseIcon: 'https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/WechatIMG193.png',
    satisfiedIcon: 'https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/WechatIMG195.png',
    dissatisfiedChooseIcon: 'https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/WechatIMG192.png',
    dissatisfiedIcon: 'https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/WechatIMG194.png',
    anonymousType: false,
    icon: [false, false, false, false, false],
    evaluationNum: 0,
    evaluationTwoNum: 0,
    evaluationThreeNum: 0,
    evaluationTitle: '非常差',
    evaluationTwoTitle: '非常差',
    evaluationThreeTitle: '非常差',
    evaluationTextarea: '',
    evaluationData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    console.log(options)
    _this.data.id = options.id
    _this.findClick(options.id)
  },

  riderClick() {
    this.data.riderStatus = !this.data.riderStatus
    this.setData({
      riderStatus: this.data.riderStatus
    })
  },
  riderTagClick(e) {
    console.log(e)
    this.data.riderTag[e.currentTarget.dataset.index].type = !this.data.riderTag[e.currentTarget.dataset.index].type
    this.setData({
      riderTag: this.data.riderTag
    })
  },
  anonymousClick() {
    console.log('111')
    this.data.anonymousType = !this.data.anonymousType
    this.setData({
      anonymousType: this.data.anonymousType
    })
  },
  productClick(data) {
    console.log(data)
    var indexs = data.target.dataset.indexs;
    this.data.evaluationNum = indexs
    // this.data.product[index].iconIndex = indexs;
    this.data.evaluationTitle = this.stars(indexs)
    // console.log(this.data.product)
    this.setData({
      evaluationNum: this.data.evaluationNum,
      evaluationTitle: this.data.evaluationTitle
    })
  },
  productTwoClick(data) {
    var indexs = data.target.dataset.indexs;
    this.data.evaluationTwoNum = indexs
    // this.data.product[index].iconIndex = indexs;
    this.data.evaluationTwoTitle = this.stars(indexs)
    // console.log(this.data.product)
    this.setData({
      evaluationTwoNum: this.data.evaluationTwoNum,
      evaluationTwoTitle: this.data.evaluationTwoTitle
    })
  },
  productThreeClick(data) {
    var indexs = data.target.dataset.indexs;
    this.data.evaluationThreeNum = indexs
    // this.data.product[index].iconIndex = indexs;
    this.data.evaluationThreeTitle = this.stars(indexs)
    // console.log(this.data.product)
    this.setData({
      evaluationThreeNum: this.data.evaluationThreeNum,
      evaluationThreeTitle: this.data.evaluationThreeTitle
    })
  },
  stars(data) {
    var starsTitle = '';
    if (data == 0) {
      starsTitle = '非常差'
    } else if (data == 1) {
      starsTitle = '差'
    } else if (data == 2) {
      starsTitle = '一般'
    } else if (data == 3) {
      starsTitle = '满意'
    } else if (data == 4) {
      starsTitle = '非常满意'
    }
    return starsTitle
  },
  textareaClick(data) {
    console.log(data)
    this.data.evaluationTextarea = data.detail.value,
      this.setData({
        evaluationTextarea: this.data.evaluationTextarea
      })
  },
  evaluateClick() {
    var _this = this
    console.log(this.data.evaluationData)
    _this.data.evaluationData.forEach(function(item){
      item.star = _this.data.evaluationNum,
      item.content = _this.data.evaluationTextarea
    })
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    save(_this.data.evaluationData).then(res => {
      wx.hideLoading()
      if (res.type == 'success') {
        wx.redirectTo({
          url: '/pages/order/evaluate/complete',
        })
      }
    }).catch(error => {
      wx.hideLoading()
      console.log(error)
    })
  },
  findClick(data) {
    var _this = this
    console.log(data)
    find(data).then(
      res => {
        console.log(res)
        if (res.type == 'success') {
          res.data.order.itemList.forEach(function (item) {
            var itemEvaluation = {
              content: '',
              itemList: [],
              name: item.name,
              orderId: item.orderId,
              price: item.price,
              productId: item.productId,
              quantity: item.quantity,
              spec: item.spec,
              star: 4,
              thumbnail: item.thumbnail,
              unit: item.unit
            }
            _this.data.evaluationData.push(itemEvaluation)
          })
          _this.setData({
            evaluationData: _this.data.evaluationData
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
})
